package net.lainiao.service.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lainiao.base.model.ShiroRole;
import net.lainiao.base.model.ShiroUser;
import net.lainiao.service.dao.ShiroRoleMapper;
import net.lainiao.service.dao.ShiroUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/1/19.
 */
@Service
public class ShiroRoleService {
    @Resource
    private ShiroRoleMapper shiroRoleMapper;
    @Resource
    private ShiroUserMapper shiroUserMapper;

    public List<ShiroRole> getAllRoles() {
        return shiroRoleMapper.getAllRoles();
    }

    public List<ShiroRole> getRolesForUser(String username) {
        ShiroUser shiroUser=shiroUserMapper.getUserByUserName(username);
        return shiroRoleMapper.getRolesForUserId(shiroUser.getId());
    }

    public int save(ShiroRole role) {
        if(role.getId()==null){
            return shiroRoleMapper.insert(role);
        }
        return shiroRoleMapper.updateByPrimaryKey(role);
    }

    public int del(int id) {
        return shiroRoleMapper.deleteByPrimaryKey(id);
    }

    public PageInfo<ShiroRole> getRolesForPage(int pageIndex) {
        PageHelper.startPage(pageIndex,10);
        List<ShiroRole>  list=shiroRoleMapper.selectAll();
        PageInfo<ShiroRole> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    public ShiroRole getRoleForId(int roleId) {
        return shiroRoleMapper.selectByPrimaryKey(roleId);
    }
}
